import { newsPageByQo } from "@/api/website";
import { getShopInfo, productPageByQo, collect } from "@/api/front-home";

const state = {
  navIndex: localStorage.getItem("xxx-nav") || 0,
  footerData: {
    //帮助中心
    about: [],
    question: [],
    jurisdiction: [],
    agreement: [],
    newGuy: []
  },
  //店铺
  shopId: "",
  shopInfo: {},
  indexCategoryData: {}
};
const mutations = {
  //帮助中心
  SET_Foot_ABOUT: (state, info) => {
    state.footerData.about = info;
  },
  SET_Foot_QUESTION: (state, info) => {
    state.footerData.question = info;
  },
  SET_Foot_JURISDICTION: (state, info) => {
    state.footerData.jurisdiction = info;
  },
  SET_Foot_AGREEMENT: (state, info) => {
    state.footerData.agreement = info;
  },
  SET_Foot_NEWGUY: (state, info) => {
    state.footerData.newGuy = info;
  },
  //店铺
  SET_SHOPID: (state, info) => {
    state.shopId = info && Number(info);
  },
  SET_SHOPINFO: (state, info) => {
    state.shopInfo = info;
  },
  SET_indexCategoryData: (state, info) => {
    state.indexCategoryData = info;
  },
  SET_NAV(state, info) {
    state.navIndex = info;
    localStorage.setItem("xxx-nav", info);
  }
};
const actions = {
  //帮助中心
  getNews({ commit, state }, code) {
    return new Promise((resolve, reject) => {
      if (
        code === "aboutPlatform" &&
        state.footerData.about &&
        state.footerData.about.length > 0
      ) {
        resolve(state.footerData.about);
      } else if (
        code === "question" &&
        state.footerData.question &&
        state.footerData.question.length > 0
      ) {
        resolve(state.footerData.question);
      } else if (
        code === "jurisdiction" &&
        state.footerData.jurisdiction &&
        state.footerData.jurisdiction.length > 0
      ) {
        resolve(state.footerData.jurisdiction);
      } else if (
        code === "agreement" &&
        state.footerData.agreement &&
        state.footerData.agreement.length > 0
      ) {
        resolve(state.footerData.agreement);
      } else if (
        code === "newGuy" &&
        state.footerData.newGuy &&
        state.footerData.newGuy.length > 0
      ) {
        resolve(state.footerData.newGuy);
      } else {
        newsPageByQo(code)
          .then(response => {
            switch (code) {
              case "aboutPlatform":
                commit("SET_Foot_ABOUT", response.list);
                break;
              case "question":
                commit("SET_Foot_QUESTION", response.list);
                break;
              case "jurisdiction":
                commit("SET_Foot_JURISDICTION", response.list);
                break;
              case "agreement":
                commit("SET_Foot_AGREEMENT", response.list);
                break;
              case "newGuy":
                commit("SET_Foot_NEWGUY", response.list);
                break;

              default:
                break;
            }
            resolve(response.list);
          })
          .catch(error => {
            reject(error);
          });
      }
    });
  },
  //店铺信息
  getShopInfo({ commit, state }, id) {
    getShopInfo(id).then(res => {
      commit("SET_SHOPINFO", res);
    });
  },

  //收藏或取消收藏
  collect({ commit }, params) {
    return new Promise((resolve, reject) => {
      collect(params)
        .then(res => {
          resolve(res);
        })
        .catch(error => {
          reject(error);
        });
    });
  }
};
export default {
  namespaced: true,
  state,
  mutations,
  actions
};
